package com.example.mybatisplusdemo.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mybatisplusdemo.common.utls.Res;
import com.example.mybatisplusdemo.model.domain.GoodOrder;
import com.example.mybatisplusdemo.model.vo.OrderDetailVo;
import com.example.mybatisplusdemo.model.vo.OrderVo;
import com.example.mybatisplusdemo.service.IOrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;
import java.util.List;


/**
 *
 *  前端控制器
 *
 *
 * @author lxp
 * @since 2025-07-15
 * @version v1.0
 */
@RestController
@RequestMapping("/api/order")
public class OrderController {

    private final Logger logger = LoggerFactory.getLogger( OrderController.class );

    @Autowired
    private IOrderService orderService;

    @PostMapping("/create")
    public Res<GoodOrder> createOrder(@RequestBody GoodOrder order) {
        boolean result = orderService.createOrder(order);
        if (result) {
            return Res.success("订单创建成功", order);
        }
        return Res.error("订单创建失败");
    }

    @PostMapping("/cancel/{id}")
    public Res<Object> cancelOrder(@PathVariable Integer id) {
        boolean result = orderService.cancelOrder(id);
        if (result) {
            return Res.success("订单取消成功", id);
        }
        return Res.error("订单取消失败");
    }

    @PostMapping("/pay/{id}")
    public Res<Object> payOrder(@PathVariable Integer id) {
        boolean result = orderService.payOrder(id);
        if (result) {
            return Res.success("订单支付成功", id);
        }
        return Res.error("订单支付失败");
    }

    @GetMapping("/detail/{id}")
    public Res<OrderDetailVo> getOrderDetail(@PathVariable Integer id) {
        OrderDetailVo orderDetail = orderService.getOrderDetail(id);
        if (orderDetail != null) {
            return Res.success("获取订单详情成功", orderDetail);
        }
        return Res.error("获取订单详情失败");
    }

    @GetMapping("/user/{userId}")
    public Res<List<OrderVo>> getOrdersByUser(@PathVariable Integer userId) {
        List<OrderVo> orders = orderService.getOrderListByUserId(userId);
        return Res.success("获取用户订单成功", orders);
    }

    @GetMapping("/status/{status}")
    public Res<List<OrderVo>> getOrdersByStatus(@PathVariable Integer status) {
        List<OrderVo> orders = orderService.getOrderListByStatus(status);
        return Res.success("获取订单列表成功", orders);
    }

    @GetMapping("/page")
    public Res<Page<GoodOrder>> page(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam(required = false) String orderNo,
            @RequestParam(required = false) Integer status) {

        Page<GoodOrder> pageInfo = new Page<>(page, pageSize);
        QueryWrapper<GoodOrder> queryWrapper = new QueryWrapper<>();

        if (orderNo != null && !orderNo.isEmpty()) {
            queryWrapper.like("order_no", orderNo);
        }

        if (status != null) {
            queryWrapper.eq("status", status);
        }

        queryWrapper.orderByDesc("create_time");

        return Res.success("获取订单分页成功", orderService.getPage(pageInfo, queryWrapper));
    }

    @PutMapping("/update")
    public Res<GoodOrder> updateOrder(@RequestBody GoodOrder order) {
        order.setUpdateTime(new Date());
        boolean result = orderService.updateById(order);
        if (result) {
            return Res.success("订单更新成功", order);
        }
        return Res.error("订单更新失败");
    }

    @DeleteMapping("/delete/{id}")
    public Res<Object> deleteOrder(@PathVariable Integer id) {
        boolean result = orderService.removeById(id);
        if (result) {
            return Res.success("订单删除成功", id);
        }
        return Res.error("订单删除失败");
    }
}

